package com.styletransfer.security;

import com.styletransfer.mapper.UserMapper;
import com.styletransfer.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import org.springframework.security.core.GrantedAuthority;

/**
 * 自定义用户详情服务
 * 
 * 这个类实现了Spring Security的UserDetailsService接口，用于加载用户信息。
 * 在用户登录时，Spring Security会调用这个类的loadUserByUsername方法来获取用户信息。
 */
@Service
public class CustomUserDetailsService implements UserDetailsService {
    
    @Autowired
    private UserMapper userMapper;
    
    /**
     * 自定义UserDetails实现，包含用户ID
     */
    public static class CustomUserDetails implements UserDetails {
        private final String username;
        private final String password;
        private final Collection<? extends GrantedAuthority> authorities;
        private final boolean enabled;
        private final Long userId;  // 添加用户ID字段
        
        public CustomUserDetails(String username, String password, 
                                Collection<? extends GrantedAuthority> authorities,
                                boolean enabled, Long userId) {
            this.username = username;
            this.password = password;
            this.authorities = authorities;
            this.enabled = enabled;
            this.userId = userId;
        }
        
        @Override
        public Collection<? extends GrantedAuthority> getAuthorities() {
            return authorities;
        }
        
        @Override
        public String getPassword() {
            return password;
        }
        
        @Override
        public String getUsername() {
            return username;
        }
        
        @Override
        public boolean isAccountNonExpired() {
            return true;
        }
        
        @Override
        public boolean isAccountNonLocked() {
            return true;
        }
        
        @Override
        public boolean isCredentialsNonExpired() {
            return true;
        }
        
        @Override
        public boolean isEnabled() {
            return enabled;
        }
        
        // 获取用户ID的方法
        public Long getUserId() {
            return userId;
        }
    }
    
    /**
     * 根据用户名加载用户信息
     * 
     * @param username 用户名
     * @return 用户详情
     * @throws UsernameNotFoundException 如果用户不存在
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 从数据库中查询用户信息
        User user = userMapper.findByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户不存在：" + username);
        }
        
        // 检查用户状态
        if (user.getStatus() != 1) {
            throw new UsernameNotFoundException("用户已被禁用：" + username);
        }
        
        // 创建并返回自定义UserDetails对象，包含用户ID
        return new CustomUserDetails(
                user.getUsername(),
                user.getPassword(),
                Collections.singletonList(new SimpleGrantedAuthority("ROLE_" + user.getRole().toUpperCase())),
                user.getStatus() == 1,
                user.getId()
        );
    }
}
